<?php

namespace Buu\Modules\Constant\Controllers;

use Adldap\Models\ModelNotFoundException;
use App\Admin\Extensions\Tools\UserButton;
use App\Http\Controllers\Controller;
use App\Models\ConstantType;
use App\Models\ConstantValue;
use App\Models\ConstantValueGroup;
use App\Models\ConstantValueGroupRelation;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
use App\Models\ConsumerChannel;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Widgets\Box;
use Encore\Admin\Widgets\Table;
use Illuminate\Http\Request;
use Illuminate\Support\MessageBag;

class ConstantValueGroupController extends Controller
{
    /**
     * title
     * @return string
     */
    protected function title()
    {
        return "常量组";
    }

    public function index(Request $request, Content $content,$id)
    {
        return $content
            ->header('常量组')
            ->breadcrumb(
                ['text' => '常量列表', 'url' => '/Constant/constant-type/list'],
                ['text' => '常量组']
            )
            ->body($this->grid($id));
    }

    /**
     * 列表构建
     *
     * @return Grid
     */
    protected function grid($id)
    {
        $grid = new Grid(new ConstantValueGroup());
        $grid->model()->where('constant_type_id',$id)->orderBy('id');
        $grid->disableFilter();
        $grid->disableExport();
        $grid->disableBatchActions();
        $grid->disableCreateButton();
        $grid->disablePagination();


        $grid->column('constant_type_id', '常量类型')->display(function () {
            return ConstantType::where('id',$this->constant_type_id)->pluck('name');
        })->label();
        $grid->column('value', '常分组常量名称');
        $grid->column('name', '分组名称');
        $grid->column('desc', '分组描述')->editable();

        $grid->column('配置')->display(function() use ($id){
            $url = '/admin/Constant/constant-value-group-relation/addnew/'.$id .'/'.$this->id;
            return "<a href= $url>常量值</a>";
        })->width(100);

        $grid->tools(function (Grid\Tools $tool)use($id) {
            $tool->append(new UserButton(UserButton::TYPE_HREF, '', '新增常量 组', '', url('/admin/Constant/constant-value-group/add/'.$id)));
        });

        // 全部关闭
        $grid->disableActions();
        return $grid;
    }

    public function add(Request $request, Content $content, $id)
    {
        $request = $request->all();
        return $content
            ->header('创建常量组')
            ->body(self::addAction($request, $id));
    }

    public function addAction($request, $id)
    {
        $aCtype = ConstantType::getOne($id)->toArray();


        $aCtypeIdName = array_column($aCtype,'name','id');
        $aIsGumper = array_column($aCtype,'is_gumper','id');
        $aGumper = array_column($aCtype,'gumper','id');
        $aValueType = array_column($aCtype,'value_type','id');

        $form = new Form(new ConstantValueGroup());
        $form->html("<span style='color:red;'>不可以修改, 请仔细填写</span>".$aCtype['name'] );



        $form->html("<p>".$aCtype['name'] ."</p>" );

        $form->hidden('constant_type_id', '常量类型接口名')->value($id);
        $form->text('value', '分组常量名称')->required()->width('300px')->rules('required')->help('大写英文或者英文和_组合，_只能在中间，不可重复')->pattern('^[A-Z]+(?:_[A-Z]+)*$');
        $form->text('name', '分组名称')->rules('required')->pattern('[\u4e00-\u9fa5]+')->width('300px')->help('中文，不可重复');
        $form->text('desc', '分组描述')->required()->width('300px');

        $form->tools(function (Form\Tools $tools) {
            // 去掉`列表`按钮
            $tools->disableList();

            // 去掉`删除`按钮
            $tools->disableDelete();

            // 去掉`查看`按钮
            $tools->disableView();
        });

        $form->footer(function ($footer) {
            // 去掉`查看`checkbox
            $footer->disableViewCheck();

            // 去掉`继续编辑`checkbox
            $footer->disableEditingCheck();

            // 去掉`继续创建`checkbox
            $footer->disableCreatingCheck();

        });
        $form->setAction('/admin/Constant/constant-value-group/addsave');
        return $form;
    }

    public function addsave(Request $request)
    {
        $request = $request->all();

        if(!empty(ConstantValueGroup::getValue($request['constant_type_id'],$request['value']))){
            $sMsg = '分组常量名称 重复';
        }
        if(!empty(ConstantValue::getDataByname($request['constant_type_id'],$request['value']))){
            $sMsg = '分组名称和常量 重复';
        }
        if(!empty(ConstantValueGroup::getName($request['constant_type_id'],$request['name']))){
            $sMsg = '分组名称 重复';
        }
        if(!empty($sMsg)){
            admin_toastr($sMsg, 'error', ['timeOut' => 5000]);
            return redirect('/admin/Constant/constant-value-group/add/'.$request['constant_type_id'])->withInput();
        }
        
        $aValue = [
            'constant_type_id' =>$request['constant_type_id'],
            'name'             =>$request['name'],
            'value'            =>$request['value'],
            'desc'             =>$request['desc'],
        ];
        if( ConstantValueGroup::insert($aValue)){
            $sMsg = 'success';
        }else{
            $sMsg = 'error';
        }

        admin_toastr($sMsg, $sMsg, ['timeOut' => 3]);
        return redirect('/admin/Constant/constant-value-group/list/'.$request['constant_type_id']);
    }

    public function listsave(Request $request)
    {
        $request = $request->all();
        $rs = [];
        if(!empty($request['value'])){
            $updateData = array(
                "desc" =>$request['value']
            );
            $rs = ConstantValueGroup::saveInfo($request['pk'],$updateData);
        }

        if($rs){
            return response()->json(['status'=>true,'message'=>'修改成功']);

        }else {
            return response()->json(['status' => false, 'message' => '修改失败']);
        }
    }
}